﻿using System;

namespace Shared.Service.EventBus.RabbitMq
{
    public class EventBusSetting
    {
        /// <summary>
        /// 是否启用队列
        /// </summary>
        public bool IsEnabled { get; set; }

        /// <summary>
        /// 服务器
        /// </summary>
        public string Server { get; set; }

        /// <summary>
        /// 虚拟主机, 用于隔离不同的模块或项目
        /// </summary>
        public string VHost { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        public int Port { get; set; }

        /// <summary>
        /// 是否启用集群
        /// </summary>
        public bool IsClusterEnabled { get; set; }

        /// <summary>
        /// 交换器
        /// </summary>
        public string Exchange { get; set; }

        #region 重试配置, 示例：消息消费失败后重试3次，从5秒到2分钟指数组增长 => config.UseRetry(r => r.Exponential(3, TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(2), TimeSpan.FromSeconds(5)));
        /// <summary>
        /// 是否启用重试
        /// </summary>
        public bool IsRetryEnabled { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryLimit { get; set; }

        /// <summary>
        /// 重试时间最小间隔, 示例：TimeSpan.FromSeconds(5)
        /// </summary>
        public TimeSpan RetryMinInterval { get; set; }

        /// <summary>
        /// 重试时间最小间隔, 示例：TimeSpan.FromMinutes(2)
        /// </summary>
        public TimeSpan RetryMaxInterval { get; set; }

        /// <summary>
        /// 重试开始的时间间隔, 示例：TimeSpan.FromSeconds(5)
        /// </summary>
        public TimeSpan RetryIntervalDelta { get; set; }
        #endregion


        #region 限速配置, 示例：每秒消息消费数限定在1000之内 => config.UseRateLimit(1000, TimeSpan.FromSeconds(1));
        /// <summary>
        /// 是否启用限速
        /// </summary>
        public bool IsRateLimitEnabled { get; set; }

        /// <summary>
        /// 指定时间段内限速次数
        /// </summary>
        public int RateLimit { get; set; }

        /// <summary>
        /// 限速评测时间段, 示例：TimeSpan.FromMinutes(1)
        /// </summary>
        public TimeSpan RateLimitInterval { get; set; }
        #endregion
        
        #region 断路器: 防止跨多个系统的这种灾难性级联故障
        /// <summary>
        /// 是否启用断路器
        /// </summary>
        public bool IsCircuitBreakerEnabled { get; set; }

        /// <summary>
        /// 重置失败次数的时间段
        /// </summary>
        public TimeSpan CircuitBreakerTrackingPeriod { get; set; } 

        /// <summary>
        /// 设定触发断路器之前失败次数达到的百分比
        /// </summary>
        public int CircuitBreakerTripThreshold { get; set; }

        /// <summary>
        /// 设定触发断路器之前必须达到的失败次数
        /// </summary>
        public int CircuitBreakerActiveThreshold { get; set; }
        #endregion

        /// <summary>
        /// RabbitMQ服务器URI
        /// </summary>
        public Uri RabbitMqUri
        {
            get
            {
                var vHost = string.Empty;
                if (!string.IsNullOrWhiteSpace(VHost))
                    vHost = VHost.TrimStart('/');

                return new Uri($"rabbitmq://{Server}:{Port}/{vHost}");
            }
        }

        public Uri EndpointUri => new Uri($"{RabbitMqUri}{Exchange}");
    }
}
